package com.yl.auth.controller;

import cn.hutool.core.util.StrUtil;
import com.yl.auth.entity.User;
import com.yl.auth.entity.Users;
import com.yl.auth.model.vo.UserVo;
import com.yl.auth.model.vo.ZhongXinVo;
import com.yl.auth.service.JigouzhongxinduanaccountService;
import com.yl.auth.service.UserService;
import com.yl.auth.service.UsersService;
import com.yl.common.result.Result;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * @author 王庆梁
 * @datetime 2023/6/15 8:29
 * 后台登录登出
 * </p>
 */
@Api(tags = "后台登录管理")
@RestController
@RequestMapping("/system/index")
@CrossOrigin
public class IndexController {

    @Resource
    private UsersService usersService;

    @PostMapping("login")
    @ApiOperation(value = "登录功能")
    public Result login(@ApiParam("用户") UserVo userVo){
        String username = userVo.getUsername();
        String password = userVo.getPassword();
        Users users = new Users();
        users.setUsername(username);
        users.setPassword(password);
        if(StrUtil.isBlank(username)||StrUtil.isBlank(password)) {
            return Result.fail();
        }
        Users user1 =usersService.login(users);
        String type = user1.getType();
        System.out.println("----------------------------------------------["+type);
        if(type.equals("1")) {
            Users primId = usersService.getPrimId(user1.getId());
            return Result.ok(primId);
        }else if(type.equals("2")){
            Users zxPrimId = usersService.getZxPrimId(user1.getId());
            return Result.ok(zxPrimId);
        }else if(type.equals("3")){
            Users yuanGong = usersService.getYuanGong(user1.getId());
            return Result.ok(yuanGong);
        }else {
            return Result.fail();
        }
    }
//    /**
//     * 登录
//     *
//     * @return
//     */
//    @PostMapping("login")
//    public Result login() {
//        Map<String, Object> map = new HashMap<>();
//        map.put("token","admin");
//        return Result.ok(map);
//    }
    /**
     * 登录
     *
     * @return
     */
//    @PostMapping("login")
//    public Result login() {
//        Map<String, Object> map = new HashMap<>();
//        map.put("token","admin");
//        return Result.ok(map);
//    }
    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("info")
    public Result info() {
        Map<String, Object> map = new HashMap<>();
        map.put("roles","[admin]");
        map.put("name","admin");
        map.put("avatar","https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
        return Result.ok(map);
    }
    /**
     * 退出
     * @return
     */
    @PostMapping("logout")
    public Result logout(){
        return Result.ok();
    }


    private static long tokenExpiration = 24*60*60*1000;

    private static String tokenSignKey = "ln123";

//    public static void main(String[] args) {
//        String token = Jwts.builder()
//                .setHeaderParam("typ", "JWT") //令牌类型
//                .setHeaderParam("alg", "HS256") //签名算法
//                .setSubject("ln-user") //令牌主题
//                .setIssuer("ln")//签发者
//                .setAudience("ln")//接收者
//                .setIssuedAt(new Date())//签发时间
//                .setExpiration(new Date(System.currentTimeMillis() + tokenExpiration)) //过期时间
//                .setNotBefore(new Date(System.currentTimeMillis() + 20*1000)) //20秒后可用
//                .setId(UUID.randomUUID().toString())
//                .claim("nickname", "zhaoyuda")
//                .claim("avatar", "123")
//                .signWith(SignatureAlgorithm.HS256, tokenSignKey)//签名哈希
//                .compact(); //转换成字符串
//        System.out.println(token);
//        //eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJsbi11c2VyIiwiaXNzIjoibG4iLCJhdWQiOiJsbiIsImlhdCI6MTY4NDkxNDgzNywiZXhwIjoxNjg1MDAxMjM3LCJuYmYiOjE2ODQ5MTQ4NTcsImp0aSI6IjZkYTI5ODdmLTEwZjYtNDM0Zi1iOTk0LWM1Y2VkMGI3ZjExNCIsIm5pY2tuYW1lIjoiemhhb3l1ZGEiLCJhdmF0YXIiOiIxMjMifQ.bw5eOfraZgBU5OLArh1skux-9piR1d3l6Us2m61efJQ
//    }
    public static void main(String[] args) {
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJsbi11c2VyIiwiaXNzIjoibG4iLCJhdWQiOiJsbiIsImlhdCI6MTY4NDkxNDgzNywiZXhwIjoxNjg1MDAxMjM3LCJuYmYiOjE2ODQ5MTQ4NTcsImp0aSI6IjZkYTI5ODdmLTEwZjYtNDM0Zi1iOTk0LWM1Y2VkMGI3ZjExNCIsIm5pY2tuYW1lIjoiemhhb3l1ZGEiLCJhdmF0YXIiOiIxMjMifQ.bw5eOfraZgBU5OLArh1skux-9piR1d3l6Us2m61efJQ";
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
        Claims claims = claimsJws.getBody();
        String subject = claims.getSubject();
        String issuer = claims.getIssuer();
        String audience = claims.getAudience();
        Date issuedAt = claims.getIssuedAt();
        Date expiration = claims.getExpiration();
        Date notBefore = claims.getNotBefore();
        String id = claims.getId();
        System.out.println(subject);
        System.out.println(issuer);
        System.out.println(audience);
        System.out.println(issuedAt);
        System.out.println(expiration);
        System.out.println(notBefore);
        System.out.println(id);;
        String nickname = (String)claims.get("nickname");
        String avatar = (String)claims.get("avatar");
        System.out.println(nickname);
        System.out.println(avatar);
    }
}
